﻿package com.shopcz.mx.shopcar.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.shopcz.dao.ShopcarDao;
import com.shopcz.mx.shopcar.service.CarService;
import com.shopcz.page.CarPage;
import com.shopcz.pojo.Address;
import com.shopcz.pojo.User;
import com.shopcz.view.CarInfoView;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

/**
 * 
 * @author mx
 *
 */

public class product {
	public String deleteCartById(HttpServletRequest request,HttpServletResponse response){
		System.out.println(1222);
		try {
			request.setCharacterEncoding("utf-8");
			int carid=Integer.parseInt(request.getParameter("no"));
			CarService carservice=new CarService();
			carservice.deleteByid(carid);
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "deleteOk";
	}
	
	
	public String deleteCart(HttpServletRequest request,HttpServletResponse response){

		try {
			request.setCharacterEncoding("utf-8");
			
			CarService carservice=new CarService();
			carservice.deleteAll();
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "deleteAllOk";
	}
	

	public String queryByUserIdPage(HttpServletRequest request,HttpServletResponse response){
		 //获取当前登录的信息:分页5个
		 response.setContentType("text/html;charset=utf-8");
		 int flag = 0;
		 String[] c = new String[]{"0","1","2","3","4"};
		 for(int i=0;i<5;i++){
			 c[i] = request.getParameter(c[i]);
		 }
		 
		 PrintWriter out;
		 try {
			 out = response.getWriter();
			 CarService carservice=new CarService();
			 for(int i=0;i<5;i++){
				 if(c[i]!=null&&!c[i].equals("0")){
					 //根据循环得到的购物车id将其状态改变为3！
					 if(!carservice.updateStatusService((Integer.parseInt(c[i])))){
						 //失败返回一个数！
						 flag = 1;
						 out.print(1);
						 out.flush();
						 out.close();
						 return null;
					 }
				 }
			 }
			 
		 } catch (IOException e) {
				e.printStackTrace();
			 };
	
		 return null;	
	}

	
	//购物结算查询
	public String queryByUserId(HttpServletRequest request,HttpServletResponse response){
	
		 //获取当前登录的信息
		  User user=(User)request.getSession().getAttribute("user_login");
		  CarService carservice=new CarService();
		  request.getSession().setAttribute("carAll", carservice.queryCarService(user.getUserId()));

		  return "queryOk";
		
		
		
	}
	
	/**
	 * 购物车分页管理
	 * @param request
	 * @param response
	 * @return
	 */
	public String carPageManage(HttpServletRequest request,HttpServletResponse response){


		int num = Integer.parseInt(request.getParameter("currentPage"));
		//获取当前登录的信息
		User user=(User)request.getSession().getAttribute("user_login");
	    //查询当前登录用户的购物车信息
		//查询当前登录用户的购物车信息		
		ShopcarDao carDao = new ShopcarDao();
		//查询到到的数据转换为 json
		CarPage carPage = carDao.queryByUserIdPage(user.getUserId(),num);
		List<CarInfoView> usercar=carPage.getData();
		  
		
		
		JSONArray jsonList = JSONArray.fromObject(usercar);
		//2.JSONObject对象转换为json格式字符串
		String jsonStr = jsonList.toString();

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(jsonStr);
			out.flush();
			out.close();	
		
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return null;
	}

	public String saveAddress(HttpServletRequest request,HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf-8");
			 //获取当前登录的信息
		    User user=(User)request.getSession().getAttribute("login_user");
		   String name= request.getParameter("name");
		   String phone=request.getParameter("phone");
		   String address=request.getParameter("address");
		   Address addr=new Address();
		   addr.setUserId(3);//user.getUserId()
		   addr.setAddrress(address);
		   addr.setConName(name);
		   addr.setConPhone(phone);
		   addr.setStatus(1);
		   CarService carservice=new CarService();
		  if(carservice.save(addr)){
			  return "saveOk";  
				
		  }
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
}
